package com.icxcc.service.impl;

import com.icxcc.constant.RabbitConstant;
import com.icxcc.service.RabbitMessageService;
import com.google.common.collect.Maps;
import org.json.simple.JSONObject;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service("rabbitMessageService")
public class RabbitMessageServiceImpl implements RabbitMessageService {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Override
    public void send(String queueName, String message) {
        rabbitTemplate.convertAndSend(queueName, message);
    }

    @Override
    public void send(String queueName, String msg, long times) {
        MessagePostProcessor processor =  (message)->{
            message.getMessageProperties().setExpiration(times+"");
            return message;
        };
        Map<String, Object> param = Maps.newHashMap();
        param.put("queueName",queueName);
        param.put("message",msg);
        rabbitTemplate.convertAndSend(RabbitConstant.DEFAULT_EXCAHNGE, RabbitConstant.DELAY_DEAD_LATTER_QUEUE, JSONObject.toJSONString(param), processor);
    }
}
